package avicit.bdp.dds.dao;


import avicit.bdp.common.utils.SpringApplicationContext;
import avicit.bdp.dds.dispatch.enums.AlertStatus;
import avicit.bdp.dds.dao.entity.Alert;
import avicit.bdp.dds.dao.entity.ProcessDefinition;
import avicit.bdp.dds.dao.entity.ProcessInstance;
import avicit.bdp.dds.dao.mapper.AlertMapper;
import avicit.platform6.commons.utils.ComUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class AlertDao extends AbstractBaseDao {

    @Autowired
    private AlertMapper alertMapper;


    @Override
    protected void init() {
        alertMapper = SpringApplicationContext.getBean(AlertMapper.class);
    }

    /**
     * insert alert
     *
     * @param alert alert
     * @return add alert result
     */
    public int addAlert(Alert alert) {
        if (StringUtils.isEmpty(alert.getId())) {
            alert.setId(ComUtil.getId());
        }
        return alertMapper.insert(alert);
    }

    /**
     * update alert
     *
     * @param alertStatus alertStatus
     * @param log         log
     * @param id          id
     * @return update alert result
     */
    public int updateAlert(Integer alertStatus, String log, String id) {
        Alert alert = alertMapper.selectByPrimaryKey(id);
        alert.setAlertStatus(alertStatus);
        alert.setLastUpdateDate(new Date());
        alert.setLog(log);
        return alertMapper.updateByPrimaryKeySelective(alert);
    }


    /**
     * MasterServer or WorkerServer stoped
     *
     * @param alertgroupId alertgroupId
     * @param host         host
     * @param serverType   serverType
     */
    public void sendServerStopedAlert(int alertgroupId, String host, String serverType) {
        Alert alert = new Alert();
        alert.setId(ComUtil.getId());
        String content = String.format("[{'type':'%s','host':'%s','event':'server down','warning level':'serious'}]",
                serverType, host);
        alert.setTitle("Fault tolerance warning");
        saveTaskTimeoutAlert(alert, content, null);
    }

    /**
     * process time out alert
     *
     * @param processInstance   processInstance
     * @param processDefinition processDefinition
     */
    public void sendProcessTimeoutAlert(ProcessInstance processInstance, ProcessDefinition processDefinition) {
        Alert alert = new Alert();
        String content = String.format("[{'id':'%d','name':'%s','event':'timeout','warnLevel':'middle'}]",
                processInstance.getId(), processInstance.getName());
        alert.setTitle("Process Timeout Warn");
        //TODO 发送用户
        saveTaskTimeoutAlert(alert, content, processInstance.getWarningUserIds());
    }

    private void saveTaskTimeoutAlert(Alert alert, String content,
                                      String warningUserIds) {
        alert.setContent(content);
        alert.setCreationDate(new Date());
        alert.setLastUpdateDate(new Date());
        alertMapper.insert(alert);
    }


    /**
     * task timeout warn
     *
     * @param processInstanceId   processInstanceId
     * @param processInstanceName processInstanceName
     * @param taskId              taskId
     * @param taskName            taskName
     */
    public void sendTaskTimeoutAlert(String warningUserIds, String processInstanceId,
                                     String processInstanceName, String taskId, String taskName) {
        Alert alert = new Alert();
        alert.setId(ComUtil.getId());
        String content = String.format("[{'process instance id':'%s','task name':'%s','task id':'%s','task name':'%s'," +
                "'event':'timeout','warnLevel':'middle'}]", processInstanceId, processInstanceName, taskId, taskName);
        alert.setTitle("Task Timeout Warn");
        saveTaskTimeoutAlert(alert, content, warningUserIds);
    }

    /**
     * list the alert information of waiting to be executed
     *
     * @return alert list
     */
    public List<Alert> listWaitExecutionAlert() {
        return alertMapper.listAlertByStatus(AlertStatus.WAIT_EXECUTION);
    }


    /**
     * for test
     *
     * @return AlertMapper
     */
    public AlertMapper getAlertMapper() {
        return alertMapper;
    }
}
